package twitter.client;

import java.util.ArrayList;

import twitter.client.adapter.AccountListAdapter;
import twitter.client.bean.TwitterAccount;
import twitter.client.util.DatabaseOpenHelper;
import twitter.client.util.Global;
import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class TwitterClientActivity extends Activity implements
		OnItemClickListener {

	private ListView accountListView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		// 読み込み中footerView
		LayoutInflater inflater = (LayoutInflater) this
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		Global.loadingFooterView = inflater.inflate(R.layout.footer_loading, null);


		accountListView = (ListView) findViewById(R.id.accountList);
		accountListView.setOnItemClickListener(this);

		DatabaseOpenHelper helper = new DatabaseOpenHelper(this);
		Global.db = helper.getWritableDatabase();

		settingAccountList();

		if (Global.accounts.isEmpty()) {
			moveToAddAccount();
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		Log.d("TwitterClientActivity", "onPause");
	}

	@Override
	protected void onRestart() {
		super.onRestart();
		Log.d("TwitterClientActivity", "onRestart");
		settingAccountList();
	}

	@Override
	protected void onResume() {
		super.onResume();
		Log.d("TwitterClientActivity", "onResume");
	}

	private void moveToAddAccount() {
		Intent intent = new Intent(this, AddAccountActivity.class);
		startActivity(intent);
	}

	private void settingAccountList() {
		Cursor cursor = null;
		try {
			cursor = Global.db.rawQuery("select * from account", null);
			Global.accounts = new ArrayList<TwitterAccount>();
			if (cursor.moveToFirst()) {
				do {
					TwitterAccount account = new TwitterAccount();
					int index = 0;

					account.setId(cursor.getString(index++));
					account.setName(cursor.getString(index++));
					account.setScreenName(cursor.getString(index++));
					account.setProfileImageUrl(cursor.getString(index++));
					account.setAccessToken(cursor.getString(index++));
					account.setAccessTokenSecret(cursor.getString(index++));

					Global.accounts.add(account);
				} while (cursor.moveToNext());
			}

		} finally {
			if (cursor != null) {
				cursor.close();
			}
		}

		if (!Global.accounts.isEmpty()) {

			AccountListAdapter adapter = new AccountListAdapter(this,
					R.layout.account_row, Global.accounts);
			accountListView.setAdapter(adapter);
		}
	}

	public void addAccountButtonOnClick(View v) {
		moveToAddAccount();
	}

	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
		Log.d("onItemClick", "position : " + position + " id : " + id);
		TwitterAccount account = Global.accounts.get(position);
		Log.d("onItemClick", "account_id : " + account.getId()
				+ " account_name : " + account.getName());
		Twitter twitter = new TwitterFactory().getInstance();
		AccessToken accessToken = new AccessToken(account.getAccessToken(),
				account.getAccessTokenSecret());
		twitter.setOAuthAccessToken(accessToken);

		Intent intent = new Intent(this, TimelineActivity.class);
		intent.putExtra("accountIndex", position);
		startActivity(intent);
	}
}